package com.gdk.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.gdk.service.ArticleService;
import com.gdk.service.CategoryService;
import com.gdk.service.LoginService;
import com.gdk.service.NavigationService;
import com.gdk.utils.XVDateUtils;


/**
 * 分类controller
 * */

@Controller
public class CategoryController {
	
	@Autowired
    private CategoryService categoryService;
	

	/**
	 * 新增分类
	 * */
	@ResponseBody
	@RequestMapping("/addCategory.do")
	public int addCategory(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		
		map.put("rc_status", 1);
		map.put("ra_time", XVDateUtils.getCurrenTimeMillis());
		System.out.println("分类插入  :  "+map);
		
		int re_status = categoryService.addCategory(map);
		System.out.println("结果 : "+re_status);
		return re_status;
	}
	
	/**
	 * 查询所有分类
	 * */
	@RequestMapping("/selectAllCategory.do")
	public String selectAllCategory(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		System.out.println("查询  :  "+map);

		List<Map<String, Object>> allCategory = null;
		
		allCategory = categoryService.selectAllCategory();

		int itemCount = allCategory.size();
		
		for (Map<String, Object> map2 : allCategory) {
			if(map2.get("ra_time") != null) {
				int p = Integer.valueOf(map2.get("ra_time").toString());
				if(p != 0) {
					map2.put("ra_time", XVDateUtils.MillisFormat(p));	
				}
			}
			
		}
		
		
		request.setAttribute("allCategory",allCategory);
		return "jsp/category/category-list";
	}
	
	/**
	 *修改前查询分类详情
	 * */
	@RequestMapping("/preSelectCategoryDetails.do")
	public String preSelectCategoryDetails(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		
		Map<String, Object> categoryDetails = categoryService.selectCategoryDetails(map);
		
		request.setAttribute("categoryDetails",categoryDetails);
		return "jsp/category/category-edit";
	}
	
	/**
	 * 修改分类
	 * */
	@ResponseBody
	@RequestMapping("/updateCategory.do")
	public int updateCategory(@RequestParam Map<String,Object> map, HttpServletRequest request) {
		System.out.println("分类 : "+map);
		int shop = categoryService.updateCategoryDetails(map);
		return shop;
	}


}